package design.mode.model.strategy;

import design.mode.dispatcher.util.test.Test;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private AsyncService asyncService;
    @Override
    public ConcurrentLinkedQueue<Test> execute(){
        LinkedList<Test> list = new LinkedList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(new Test("2024-10-25", "A_" + i,i++));
        }
        for (int i = 0; i < 2000; i++) {
            list.add(new Test("2024-10-24", "B_" + i,i++));
        }
        for (int i = 0; i < 4000; i++) {
            list.add(new Test("2024-10-26", "C_" + i,i++));
        }
        for (int i = 0; i < 3000; i++) {
            list.add(new Test("2024-10-21", "C_" + i,i++));
        }
        for (int i = 0; i < 1000; i++) {
            list.add(new Test("2024-10-21", "C_" + i,i++));
        }
        for (int i = 0; i < 2000; i++) {
            list.add(new Test("2024-09-29", "C_" + i,i++));
        }
        for (int i = 0; i < 1500; i++) {
            list.add(new Test("2024-09-28", "C_" + i,i++));
        }
        for (int i = 0; i < 1100; i++) {
            list.add(new Test("2024-09-21", "C_" + i,i++));
        }

        int batchSize = 10000;
        long start = System.currentTimeMillis();
        ConcurrentLinkedQueue<Test> resultList = new ConcurrentLinkedQueue<>();

        // 使用 ConcurrentHashMap 进行分区操作
        Map<String, List<Test>> collect = new ConcurrentHashMap<>();
        list.forEach(test -> collect.computeIfAbsent(test.getDate(), k -> new ArrayList<>()).add(test));

        List<CompletableFuture<List<Test>>> futures = new ArrayList<>();

        for (List<Test> value : collect.values()) {
            List<List<Test>> partition = partitionList(value, batchSize);
            for (List<Test> subList : partition) {
                CompletableFuture<List<Test>> future = asyncService.execute(subList);
                futures.add(future);
            }
        }

        // 等待所有异步任务完成并收集结果
        for (CompletableFuture<List<Test>> future : futures) {
            try {
                // 获取结果，阻塞直到任务完成
                List<Test> result = future.join();
                if (result != null) {
                    for (Test test : result) {
                        System.out.println(test);
                    }
                }
            } catch (Exception e) {
                // 处理异常，避免中断整个流程
                e.printStackTrace();
            }
        }

        return new ConcurrentLinkedQueue<>();
    }

    private List<List<Test>> partitionList(List<Test> list, int batchSize) {
        List<List<Test>> partitions = new ArrayList<>();
        for (int i = 0; i < list.size(); i += batchSize) {
            partitions.add(list.subList(i, Math.min(i + batchSize, list.size())));
        }
        return partitions;
    }
}
